Completed
Push — master ( 58c55f...9ab122 )
by greg
02:36
created

attributes.js ➔ getAll   C

Complexity

Conditions 7
Paths 128

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
c 1
b 0
f 0
nc 128
dl 0
loc 44
rs 5.5
nop 2
1
import {
2
  Hooks
3
} from '../../'
4
5
/**
6
* Get All attributes from a Abe tag
7
* @return {Object} parsed attributes
8
*/
9
export function getAll(str, json) {
10
  str = Hooks.instance.trigger('beforeAbeAttributes', str, json)
11
12
  //This regex analyzes all attributes of a Abe tag 
13
  var re = /\b([a-z][a-z0-9\-]*)\s*=\s*("([^"]+)"|'([^']+)'|(\S+))/ig
14
  
15
  var attrs = {
16
    autocomplete: null,
17
    desc: '',
18
    display: null,
19
    editable: true,
20
    key: '',
21
    'max-length': null,
22
    'min-length': 0,
23
    order: 0,
24
    prefill: false,
25
    'prefill-quantity': null,
26
    reload: false,
27
    required: false,
28
    source: null,
29
    tab: 'default',
30
    type: 'text',
31
    value: '',
32
    file: '',
33
    visible: true
34
  }
35
  
36
  for (var match; match = re.exec(str); ){
37
    attrs[match[1]] = match[3] || match[4] || match[5]
38
  }
39
40
  attrs.sourceString = attrs.source
41
  attrs.source = (attrs.source)? 
42
    ((json != null && json['abe_source'] != null)? 
43
      json['abe_source'][attrs.key] : 
44
      null
45
    ) : 
46
    null
47
  attrs.editable = (attrs.editable && attrs.editable !== 'false') ? true : false
48
49
  attrs = Hooks.instance.trigger('afterAbeAttributes', attrs, str, json)
50
51
  return attrs
52
}
53
54
export function sanitizeSourceAttribute(obj, jsonPage){
0 ignored issues
show
Unused Code introduced by
The parameter jsonPage is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
55
  if(obj.sourceString != null && obj.sourceString.indexOf('{{') > -1) {
56
    var matches = obj.sourceString.match(/({{[a-zA-Z._]+}})/g)
57
    if(matches !== null) {
58
      Array.prototype.forEach.call(matches, (match) => {
59
        var val = match.replace('{{', '')
60
        val = val.replace('}}', '')
61
        
62
        try {
63
          val = eval('jsonPage.' + val)
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
64
        }catch(e) {
65
          val = ''
66
        }
67
        obj.sourceString = obj.sourceString.replace(match, val)
68
      })
69
    }
70
  }
71
72
  return obj
73
}